package ru.gerunn.parsing;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.hibernate.Session;

import ru.gerunn.hibernate.util.HibernateUtil;
import ru.gerunn.parsing.service.LogHandler;
import ru.gerunn.parsing.service.LogParser;
import ru.gerunn.parsing.service.LogProvider;
import ru.gerunn.parsing.tables.ETDLog;
import ru.gerunn.parsing.tables.ETDOperation;
import ru.gerunn.parsing.tables.ETDType;
import ru.gerunn.parsing.tables.MEKLog;
import ru.gerunn.parsing.tables.MEKOperation;
import ru.gerunn.parsing.tables.MEKType;
import ru.gerunn.parsing.tables.SNTLog;
import ru.gerunn.parsing.tables.SNTOperation;
import ru.gerunn.parsing.tables.SNTType;

public class Main {
	
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		
		LogParser<ETDType, ETDOperation, ETDLog> etdCommonsQueryLoggingListenerParser = new LogParser<ETDType, ETDOperation, ETDLog>(
			session,
			"CommonsQueryLoggingListener",
			Pattern.compile("^(.*),\\d\\d\\d WARN \\[net\\.ttddyy\\.dsproxy\\.listener\\.CommonsQueryLoggingListener] - ;(\\d+) ;.*; \\[(.*)\\] ;(.*)$"),
			ETDType.class,
			ETDOperation.class,
			ETDLog.class
		);

		LogParser<MEKType, MEKOperation, MEKLog> mekCommonsQueryLoggingListenerParser = new LogParser<MEKType, MEKOperation, MEKLog>(
			session,
			"CommonsQueryLoggingListener",
			Pattern.compile("^(.*),\\d\\d\\d WARN \\[net\\.ttddyy\\.dsproxy\\.listener\\.CommonsQueryLoggingListener\\].*Time:(\\d+).*\\{\\[(.*)\\](\\[.*)$"),
			MEKType.class,
			MEKOperation.class,
			MEKLog.class
		);

		LogParser<SNTType, SNTOperation, SNTLog> sntCommonsQueryLoggingListenerParser = new LogParser<SNTType, SNTOperation, SNTLog>(
			session,
			"CommonsQueryLoggingListener",
			Pattern.compile("^(.*),\\d\\d\\d WARN \\[net\\.ttddyy\\.dsproxy\\.listener\\.CommonsQueryLoggingListener\\].*Time:(\\d+).*\\{\\[(.*)\\](\\[.*)$"),
			SNTType.class,
			SNTOperation.class,
			SNTLog.class
		);

		List<LogParser<ETDType, ETDOperation, ETDLog>> etdParsers = new ArrayList<LogParser<ETDType, ETDOperation, ETDLog>>();
		etdParsers.add(etdCommonsQueryLoggingListenerParser);

		List<LogParser<MEKType, MEKOperation, MEKLog>> mekParsers = new ArrayList<LogParser<MEKType, MEKOperation, MEKLog>>();
		mekParsers.add(mekCommonsQueryLoggingListenerParser);

		List<LogParser<SNTType, SNTOperation, SNTLog>> sntParsers = new ArrayList<LogParser<SNTType, SNTOperation, SNTLog>>();
		sntParsers.add(sntCommonsQueryLoggingListenerParser);

		LogProvider etdLogProvider = new LogProvider(
			"smb://10.0.0.145/public/Backups/WAS_logs.ETD/",
			"ETD-WARN",
			etdCommonsQueryLoggingListenerParser.getTimestamp()
		);

		LogProvider mekLogProvider = new LogProvider(
			"smb://10.0.0.145/public/Backups/WAS_logs.MEK/",
			"ETD-WARN",
			mekCommonsQueryLoggingListenerParser.getTimestamp()
		);

		LogProvider sntLogProvider = new LogProvider(
			"smb://10.0.0.145/public/Backups/WAS_logs.SNT/SNT/",
			"SNT-WARN",
			sntCommonsQueryLoggingListenerParser.getTimestamp()
		);

		LogHandler<ETDType, ETDOperation, ETDLog> etdLogHandler = new LogHandler<ETDType, ETDOperation, ETDLog>(
			etdLogProvider,
			etdParsers
		);

		LogHandler<MEKType, MEKOperation, MEKLog> mekLogHandler = new LogHandler<MEKType, MEKOperation, MEKLog>(
			mekLogProvider,
			mekParsers
		);

		LogHandler<SNTType, SNTOperation, SNTLog> sntLogHandler = new LogHandler<SNTType, SNTOperation, SNTLog>(
			sntLogProvider,
			sntParsers
		);

		etdLogHandler.handle();
		mekLogHandler.handle();
		sntLogHandler.handle();
		
		HibernateUtil.shutdown();
	}

}
